First letter is capital


Posted by Christy on 2022-04-19

Description: Write a function named isUpperCase that accepts a string and returns true or false to judge if the first letter of the string is uppercase, if yes return true, if no return false

Note: Don't use built-in function toUpperCase()

function isUpperCase(str) {

}

console.log(isUpperCase("abcd")); // false
console.log(isUpperCase("Abcd")); // true
console.log(isUpperCase("ABCD")); // true
console.log(isUpperCase("aBCD")); // false

Answer:

function isUpperCase(str) {
  if (str[0] >= "A" && str[0] <= "Z") return true;
  return false;
}

Reflection:

The key is to know the comparison of alphabetical order could be higher or lower in both uppercase and lowercase letters.

For example:

ASCII Printable Characters

A: 65, B:66, C:67

B > A, C > B and so on...

Another answer:

function isUpperCase(str) {
  return str[0] >= "A" && str[0] <= "Z";
}









Related Posts

[ Nuxt.js 2.x 系列文章 ] VueX Store 搭配 vuex-persistedstate 狀態保存工具

[ Nuxt.js 2.x 系列文章 ] VueX Store 搭配 vuex-persistedstate 狀態保存工具

Chucker --- View the interaction between the app and the api

Chucker --- View the interaction between the app and the api

用 Google Calendar 與 nivo 製作自己的年終檢討報告

用 Google Calendar 與 nivo 製作自己的年終檢討報告


Comments